package com.zhang.third.day06;

import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @title:
 * @author: zhang
 * @date: 2022/4/9 14:53
 */
public class Example5 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<String> result = env
                .addSource(new SourceFunction<String>() {
                    @Override
                    public void run(SourceContext<String> ctx) throws Exception {
                        ctx.collectWithTimestamp("a", 1000L);
                        ctx.collectWithTimestamp("b", 2000L);
                        ctx.emitWatermark(new Watermark(1999L));
                        ctx.collectWithTimestamp("c", 1500L);
                    }

                    @Override
                    public void cancel() {

                    }
                })
                .process(new ProcessFunction<String, String>() {
                    @Override
                    public void processElement(String value, ProcessFunction<String, String>.Context ctx, Collector<String> out) throws Exception {
                        if (ctx.timestamp() < ctx.timerService().currentWatermark()) {
                            ctx.output(
                                    new OutputTag<String>("later-event") {
                                    },
                                    // 向侧输出流中要发送的数据
                                    "数据" + value + "迟到了，数据的事件时间是：" + ctx.timestamp() + "，" +
                                            "当前process算子的水位线是：" + ctx.timerService().currentWatermark()
                            );
                        } else {
                            out.collect("数据" + value + "没迟到。");
                        }
                    }
                });

        result.print("主流");
        result.getSideOutput(new OutputTag<String>("later-event") {
        }).print("测输出流");


        env.execute();
    }
}
